package beanAction;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import modele.beanEntity.Comment;
import modele.beanEntity.Recipe;
import modele.mapper.CommentMapper;
import modele.mapper.RecipeMapper;

@ManagedBean(name="searchRecipe")
@SessionScoped
public class SearchRecipeBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -525554025480742582L;
	private int preparationDuration;
	private int cookExpertise;
	private String cookType;
	private int nbOfPerson;
	private List<Recipe> foundRecipes = new ArrayList<Recipe>();
	private List<String> cookTypes = new ArrayList<String>();
	private Recipe selectedRecipe = null;
	private List<Comment> selectedRecipeComments = new ArrayList<Comment>();
	private String imgCookType = "/img/cook_type.png";
	private String imgPreparationDuration = "/img/preparation_duration.png";
	private String imgCookExpertise = "/img/cook_expertise.png";
	private String imgNbOfPerson = "/img/nb_of_person.png";
	
	public SearchRecipeBean() {
		try {
			RecipeMapper rm = new RecipeMapper();
			cookTypes = rm.getAllCookType();
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Impossible de récupérer la liste des types de recette", null));
		}
	}
	
	public String search() {
		RecipeMapper rm;
		try {
			rm = new RecipeMapper();
			foundRecipes = rm.searchRecipe(preparationDuration, cookExpertise, cookType, nbOfPerson);
			if(foundRecipes.size()<1) {
				FacesContext.getCurrentInstance().addMessage("searchRecipeForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Aucune recette ne correspond à vos critères", null));
			} else {
				return "recipeSearchResult.jsf";
			}
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage("searchRecipeForm", new FacesMessage(FacesMessage.SEVERITY_ERROR, "La recherche a échoué, veuillez vérifier les critères de votre recherche", null));
		}
		return "recipe.jsf";
	}
	
	public void refreshComments() {
		if(selectedRecipe != null)  {
			try {
				CommentMapper cm = new CommentMapper();
				this.selectedRecipeComments = cm.getRecipeCommentList(selectedRecipe.getId());
			} catch(Exception e) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "La recherche des commentaires associés à cette recette a échoué\n" + e.getMessage(), null));
			}
		} else {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Aucune recette n'a été choisie\n", null));
		}
	}
	
	public void refreshCookTypes() {
		try {
			RecipeMapper rm = new RecipeMapper();
			cookTypes = rm.getAllCookType();
		} catch (Exception e) {
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Impossible de récupérer la liste des types de recette", null));
		}
	}

	public int getPreparationDuration() {
		return preparationDuration;
	}

	public void setPreparationDuration(int preparationDuration) {
		this.preparationDuration = preparationDuration;
	}

	public int getCookExpertise() {
		return cookExpertise;
	}

	public void setCookExpertise(int cookExpertise) {
		this.cookExpertise = cookExpertise;
	}

	public String getCookType() {
		return cookType;
	}

	public void setCookType(String cookType) {
		this.cookType = cookType;
	}

	public int getNbOfPerson() {
		return nbOfPerson;
	}

	public void setNbOfPerson(int nbOfPerson) {
		this.nbOfPerson = nbOfPerson;
	}

	public List<Recipe> getFoundRecipes() {
		return foundRecipes;
	}

	public void setFoundRecipes(List<Recipe> foundRecipes) {
		this.foundRecipes = foundRecipes;
	}

	public List<String> getCookTypes() {
		return cookTypes;
	}

	public void setCookTypes(List<String> cookTypes) {
		this.cookTypes = cookTypes;
	}
	
	public Recipe getSelectedRecipe() {
		return selectedRecipe;
	}
	
	public void setSelectedRecipe(Recipe selectedRecipe) {
		this.selectedRecipe = selectedRecipe;
		if(selectedRecipe != null)  {
			try {
				CommentMapper cm = new CommentMapper();
				this.selectedRecipeComments = cm.getRecipeCommentList(selectedRecipe.getId());
			} catch(Exception e) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "La recherche des commentaires associés à cette recette a échoué\n" + e.getMessage(), null));
			}
		}
	}

	public String getImgCookType() {
		return imgCookType;
	}

	public void setImgCookType(String imgCookType) {
		this.imgCookType = imgCookType;
	}

	public String getImgPreparationDuration() {
		return imgPreparationDuration;
	}

	public void setImgPreparationDuration(String imgPreparationDuration) {
		this.imgPreparationDuration = imgPreparationDuration;
	}

	public String getImgCookExpertise() {
		return imgCookExpertise;
	}

	public void setImgCookExpertise(String imgCookExpertise) {
		this.imgCookExpertise = imgCookExpertise;
	}

	public String getImgNbOfPerson() {
		return imgNbOfPerson;
	}

	public void setImgNbOfPerson(String imgNbOfPerson) {
		this.imgNbOfPerson = imgNbOfPerson;
	}

	public List<Comment> getSelectedRecipeComments() {
		return selectedRecipeComments;
	}

	public void setSelectedRecipeComments(List<Comment> selectedRecipeComments) {
		this.selectedRecipeComments = selectedRecipeComments;
	}
}
